<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="产品名称">
              <JInput placeholder="请输入产品名称" v-model="queryParam.name"></JInput>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="类型">
              <j-dict-select-tag placeholder="请选择类型" v-model="queryParam.type" dictCode="product_type"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-has="'product:add'" type="primary" icon="plus">新增</a-button>
      <a-button  v-has="'product:export'" type="primary" icon="download" @click="handleExportXls('产品表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button v-has="'product:import'" type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'product:delete'"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px" v-has="'product:batch_delete'"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div>
        <a-tabs default-active-key="2" @change="tabsChange">
          <a-tab-pane key="2" tab="全部" force-render></a-tab-pane>
          <a-tab-pane key="1" tab="上架"></a-tab-pane>
          <a-tab-pane key="0" tab="下架"></a-tab-pane>
        </a-tabs>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        //产品详情
        <template slot="name" slot-scope="text, record">
          <a v-html="text" @click="handleDetail(record)" color="blue"></a>
        </template>
        <template slot="number" slot-scope="text, record">
          <a v-html="text" @click="handleDetail(record)" color="blue"></a>
        </template>
        //上下架按钮
        <template slot="isSale_dictText" slot-scope="text, record">
          <a-popconfirm title="确定上架吗?" @confirm="() => handlePutOn(record)" v-if="record.isSale == 0">
            <a-tag color='#2db7f5'>上架</a-tag>
          </a-popconfirm>
          <a-popconfirm title="确定下架吗?" @confirm="() => handlePutOn(record)" v-if="record.isSale == 1">
            <a-tag color='#2db7f5'>下架</a-tag>
          </a-popconfirm>
        </template>
        //标签
        <template slot="lableIds_dictText" slot-scope="text, record">
          <a-tag v-html="text" v-model="visible" closable ></a-tag>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'product:update'">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a v-has="'product:delete'">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <div class='buttonStyle'>
      <div class="ant-alert ant-alert-info">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a-divider class="btn" type="vertical" />
        <a  @click="onClearSelected">清空</a>
        <a-button  type="default" @click="MAction(true,'/product/product/updateByVisiblePerson',1,1260)"v-has="'product:alloter_user'">分配员工</a-button>
        <a-button  type="default" @click="MAction(true,'/product/product/deleteByVisiblePerson',2,1260)"v-has="'product:delete_user'">删除员工</a-button>
        <a-button  type="default" @click="openModal('add')" v-has="'product:alloter_department'">分配部门</a-button>
        <a-button  type="default" @click="openModal('del')" v-has="'product:delete_department'">删除部门</a-button>
        <a-button  type="danger"  @click="batchDel" v-has="'product:batch_delete'">删除</a-button>
      </div>
    </div>
    <product-modal ref="modalForm" @ok="modalFormOk"></product-modal>
    <JSelectUserByDepModalCustom ref="MAmodel" :modalWidth="MAwidth" :multi="MAisMulti" @ok="MACallback"></JSelectUserByDepModalCustom>
    <JSelectDepartModalDpt ref="innerDepartSelectModal" :multi="true" @ok="handleOK" ></JSelectDepartModalDpt>
  </a-card>
</template>
<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { postAction, putAction } from '@api/manage'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JSelectDepartModalDpt from '@/components/jeecgbiz/modal/JSelectDepartModalDpt'
import ProductModal from './modules/ProductModal.Style#Drawer'
import JSelectUserByDepModalCustom from '@/components/jeecgbiz/modal/JSelectUserByDepModalCustom'
import JInput from '@comp/jeecg/JInput'

export default {
  name: 'ProductList',
  mixins:[JeecgListMixin, mixinDevice],
  components: {
    ProductModal,
    JSelectDepartModalDpt,
    JSelectUserByDepModalCustom,
    JInput,
  },
  data () {
    return {
      visible: true,
      //判断是分配部门还是删除部门
      judgee:'',
      //名单选择时 多选还是单选
      MAisMulti:false,
      //选择名单回调接口
      MAcallBackUrl:'',
      //名单参数类型    1===移交 2===负责人 3协同人 4===分配
      MAtype:0,
      //弹框宽度
      MAwidth:1260,
      isFiledButtonShow:true,//归档 取消归档按钮
      filed:0,//搜索区 归档默认值
      description: '产品表管理页面',
      // 表头
      columns: [
        {
          title:'操作',
          align:"center",
          dataIndex: 'isSale_dictText',
          scopedSlots: { customRender: 'isSale_dictText' },
        },
        {
          title:'产品编号',
          align:"center",
          dataIndex: 'number',
          scopedSlots: { customRender: 'number' },
        },
        {
          title:'产品名称',
          align:"center",
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' },
        },
        {
          title:'产品条码',
          align:"center",
          dataIndex: 'barCode'
        },
        {
          title:'产品图片',
          align:"center",
          dataIndex: 'pictureUrl',
          scopedSlots: {customRender: 'imgSlot'}
        },
        {
          title:'分类',
          align:"center",
          dataIndex: 'type_dictText'
        },
        {
          title:'标签',
          align:"center",
          dataIndex: 'lableIds_dictText',
          scopedSlots: {customRender: 'lableIds_dictText'}
        },
        {
          title:'产品单位',
          align:"center",
          dataIndex: 'unit_dictText'
        },
        {
          title:'单价',
          align:"center",
          dataIndex: 'unitPrice'
        },
        {
          title:'成本',
          align:"center",
          dataIndex: 'cost'
        },
        // {
        //   title:'库存数量',
        //   align:"center",
        //   dataIndex: 'quantity'
        // },
        {
          title:'可见部门',
          align:"center",
          dataIndex: 'department_dictText',
        },
        {
          title:'可见人',
          align:"center",
          dataIndex: 'visiblePerson_dictText',
        },
        {
          title:'附件',
          align:"center",
          dataIndex: 'files',
          scopedSlots: {customRender: 'fileSlot'}
        },
        {
          title:'规格',
          align:"center",
          dataIndex: 'specifications'
        },
        {
          title:'创建人',
          align:"center",
          dataIndex: 'createBy'
        },
        {
          title:'创建时间',
          align:"center",
          dataIndex: 'createTime'
        },
        {
          title:'更新时间',
          align:"center",
          dataIndex: 'updateTime'
        },
        {
          title:'上架时间',
          align:"center",
          dataIndex: 'saleTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/product/product/list",
        delete: "/product/product/delete",
        deleteBatch: "/product/product/deleteBatch",
        exportXlsUrl: "/product/product/exportXls",
        importExcelUrl: "product/product/importExcel",
        update: "product/product/updateIsSale",//上下架
        updateByVisiblePerson:"product/product/updateByVisiblePerson",//添加员工
        deleteByVisiblePerson:"product/product/deleteByVisiblePerson",//删除员工
        updateByDepartment:"product/product/updateByDepartment",//分配部门
        deleteByDepartment:"/product/product/deleteByDepartment",//删除部门
      },
      dictOptions:{},
      superFieldList:[],
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    initDictConfig(){
    },
    getSuperFieldList(){
      let fieldList=[];
      fieldList.push({type:'int',value:'number',text:'产品编号',dictCode:''})
      fieldList.push({type:'string',value:'name',text:'产品名称',dictCode:''})
      fieldList.push({type:'string',value:'barCode',text:'条码',dictCode:''})
      fieldList.push({type:'string',value:'pictureUrl',text:'图片',dictCode:''})
      fieldList.push({type:'int',value:'type',text:'类型(1.软件 2.其他)',dictCode:'product_type'})
      fieldList.push({type:'string',value:'lableIds',text:'标签',dictCode:''})
      fieldList.push({type:'int',value:'unit',text:'单位(1.件 2.个)',dictCode:'product_unit'})
      fieldList.push({type:'BigDecimal',value:'unitPrice',text:'单价',dictCode:''})
      fieldList.push({type:'BigDecimal',value:'cost',text:'成本',dictCode:''})
      fieldList.push({type:'string',value:'productIntroduction',text:'产品简介',dictCode:''})
      fieldList.push({type:'string',value:'files',text:'附件',dictCode:''})
      fieldList.push({type:'string',value:'specifications',text:'规格',dictCode:''})
      fieldList.push({type:'sel_depart',value:'department',text:'部门'})
      fieldList.push({type:'int',value:'isSale',text:'是否上架',dictCode:'is_sale'})
      this.superFieldList = fieldList
    },
    //上下架
    handlePutOn(record) {
      if (!this.url.update) {
        this.$message.error('请设置上架商品的属性!')
        return
      } else
      {
        if(record.isSale == 0){
          putAction(this.url.update, {id: record.id, isSale: 1 }).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.warning(res.message)
            }
          })
        }else
        {
          putAction(this.url.update, {id: record.id, isSale: 0 }).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.warning(res.message)
            }
          })
        }

      }
    },
    //分配可见人
    MAction(MAisMulti,MAcallBackUrl,MAtype,MAwidth){
      //Member action
      //设置多选还是单选
      // this.MAisMulti = false
      this.MAisMulti = MAisMulti
      //设置回调接口
      // this.MAcallBackUrl  = '/quotation/quotation/updateDirector'
      this.MAcallBackUrl  = MAcallBackUrl
      //设置回调接口参数
      // this.MAtype = 1
      this.MAtype = MAtype
      //弹框宽度
      // this.MAwidth = 1260
      this.MAwidth = MAwidth
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请至少或者最多选择一条记录！');
        return;
      }
      this.$refs.MAmodel.showModal()
    },
    MACallback(e){
      this.loading = true
      console.log('this.selectedRowKeys',this.selectedRowKeys)
      let ids = this.selectedRowKeys.join(',')
      console.log('e',e)
      let tempArr = e;
      let idArr = tempArr.map(e => {
        return e.id
      })
      let visiblePerson_idArr = idArr.join(',')
      let obj = {}
      switch (this.MAtype) {
        case 1://添加员工
          obj = {id:ids,visiblePerson:visiblePerson_idArr}
          break;
        case 2://删除员工
          obj = {id:ids,visiblePerson:visiblePerson_idArr}
          break;
      }
      postAction(this.MAcallBackUrl,obj).then(res=>{
        console.log('操作成功后',res)
        if(res.success){
          this.selectedRowKeys = []
          this.$message.success(res.message)
          this.loading = false
          this.loadData()
        }else {
          this.$message.success(res.message)
          this.loading = false
          // this.loadData()
        }
      })
    },
    //操作部门
    openModal(id){
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请至少或者最多选择一条记录！');
        return;
      }
      if (id=='add'){
        this.judgee='add'
      }else
      {
        this.judgee='del'
      }
      this.$refs.innerDepartSelectModal.show(this.selectedRowKeys)
    },
    handleOK(rows) {
      let ids = this.selectedRowKeys.join(',')
      let dptId = rows.map(e => {
        return e.id
      })
      let dptIdArr = dptId.join(',')
      let obj = {id:ids,department:dptIdArr}
      if(this.judgee=='add'){
        putAction(this.url.updateByDepartment,obj).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.warning(res.message)
          }
        })
      }
      if(this.judgee=='del'){
        putAction(this.url.deleteByDepartment,obj).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.warning(res.message)
          }
        })
      }
    },
    //tab页查询
    tabsChange(e) {
      console.log(e)
      if (e == 2) {
        this.queryParam = {}
        this.loadData()
      } else {
        this.queryParam.statusTop = e
        this.loadData()
      }
    },
  }
}
</script>
<style scoped lang="less">
@import '~@assets/less/common.less';
.buttonStyle{

  button{
    margin-left: 10px;
  }
}
</style>